package com.itboye.jinpuyiyuan.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.itboye.jinpuyiyuan.R;
import com.itboye.jinpuyiyuan.bean.OptionMenuBean;
import com.itboye.jinpuyiyuan.bean.TestServerFixedBean;
import com.itboye.jinpuyiyuan.bean.TestServerOptionBean;
import com.itboye.jinpuyiyuan.interfaces.IRecordGoodsCart;
import com.itboye.jinpuyiyuan.utils.sputil.Const;
import com.itboye.jinpuyiyuan.utils.sputil.SPUtils;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by Administrator on 2017/12/13 0013.
 */

public class TestServerOptionRightAdapter extends BaseAdapter {
    private ArrayList<OptionMenuBean.Depts.Items> list;
    private Context context;
    private LayoutInflater inflater;
    private int clickPosition;
    private IRecordGoodsCart cart;
    private String deptId;

    public  TestServerOptionRightAdapter(Context context,IRecordGoodsCart cart,ArrayList<OptionMenuBean.Depts.Items> list,String deptId){
        this.list=list;
        this.context=context;
        this.deptId=deptId;
        this.cart=cart;
        inflater=LayoutInflater.from(context);

    }


    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
       final MyViewHolder myViewHolder;
        if (convertView==null){
            myViewHolder=new MyViewHolder();
            convertView=inflater.inflate(R.layout.item_testserver_optionrightlist,parent,false);

            myViewHolder.name=convertView.findViewById(R.id.name);
            myViewHolder.price=convertView.findViewById(R.id.price);
            myViewHolder.add=convertView.findViewById(R.id.add);
            myViewHolder.delete=convertView.findViewById(R.id.delete);
            myViewHolder.numTv=convertView.findViewById(R.id.numTv);
            convertView.setTag(myViewHolder);
        }else {
            myViewHolder= (MyViewHolder) convertView.getTag();
        }
        myViewHolder.name.setText(list.get(position).getItemname());
        myViewHolder.price.setText("¥"+list.get(position).getItemprice());
        myViewHolder.numTv.setText(list.get(position).getSelects()+"");
        //数据添加到map

        if ("0".equals(myViewHolder.numTv.getText().toString())||"".equals(myViewHolder.numTv.getText().toString())){
            myViewHolder.numTv.setVisibility(View.GONE);
            myViewHolder.delete.setVisibility(View.GONE);
            myViewHolder.add.setVisibility(View.VISIBLE);
        }else {
            myViewHolder.numTv.setVisibility(View.VISIBLE);
            myViewHolder.delete.setVisibility(View.VISIBLE);
            myViewHolder.add.setVisibility(View.INVISIBLE);
        }


        myViewHolder.add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                myViewHolder.numTv.setVisibility(View.VISIBLE);
                myViewHolder.delete.setVisibility(View.VISIBLE);
                Log.d("add===",myViewHolder.numTv.getText().toString());
                int integer = Integer.valueOf(myViewHolder.numTv.getText().toString());
                myViewHolder.numTv.setText(integer+1+"");

                if (myViewHolder.numTv.getText().toString().equals("1")){
                    myViewHolder.add.setVisibility(View.INVISIBLE);
                }else {
                    myViewHolder.add.setVisibility(View.VISIBLE);
                }
                //map数据更新
//                TestServerOptionRightAdapter.this.notifyDataSetChanged();
                cart.onChange(deptId,position,true);

            }
        });
        myViewHolder.delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int integer = Integer.valueOf(myViewHolder.numTv.getText().toString());
                if ("1".equals(myViewHolder.numTv.getText().toString())){
                    myViewHolder.numTv.setVisibility(View.GONE);
                    myViewHolder.delete.setVisibility(View.GONE);
                }
                myViewHolder.numTv.setText(integer-1+"");

                if (myViewHolder.numTv.getText().toString().equals("0")){
                    myViewHolder.add.setVisibility(View.VISIBLE);
                }else {
                    myViewHolder.delete.setVisibility(View.VISIBLE);
                }
//                TestServerOptionRightAdapter.this.notifyDataSetChanged();

                //map数据更新
                cart.onChange(deptId,position,false);
            }
        });
        return convertView;
    }

//添加right数据和购物车显示相关的view
 public void addData(ArrayList<OptionMenuBean.Depts.Items> list,String deptId){
        this.list=list;
        this.deptId=deptId;
        this.notifyDataSetChanged();
        }

    class MyViewHolder{
        TextView name,price,numTv;
        ImageView add,delete;

    }


}
